﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Threading.Channels;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using ZR.Service.Service.ProductionControl;

//创建时间：2025-09-10
namespace ZR.Admin.WebApi.Controllers {
    /// <summary>
    /// 采集曲线数据
    /// </summary>
    [Verify]
    [Route("business/CksCollectedCurveData")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksCollectedCurveDataController : BaseController {
        /// <summary>
        /// 采集曲线数据接口
        /// </summary>
        private readonly ICksCollectedCurveDataService _CksCollectedCurveDataService;

        public CksCollectedCurveDataController(ICksCollectedCurveDataService CksCollectedCurveDataService) {
            _CksCollectedCurveDataService = CksCollectedCurveDataService;
        }

        /// <summary>
        /// 查询采集曲线数据列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:list")]
        public IActionResult QueryCksCollectedCurveData([FromQuery] CksCollectedCurveDataQueryDto parm) {
            var response = _CksCollectedCurveDataService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询采集曲线数据详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:query")]
        public IActionResult GetCksCollectedCurveData(int Id) {
            var response = _CksCollectedCurveDataService.GetInfo(Id);

            var info = response.Adapt<CksCollectedCurveDataDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加采集曲线数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:add")]
        [Log(Title = "采集曲线数据", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksCollectedCurveData([FromBody] CksCollectedCurveDataDto parm) {
            var modal = parm.Adapt<CksCollectedCurveData>().ToCreate(HttpContext);

            var response = _CksCollectedCurveDataService.AddCksCollectedCurveData(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新采集曲线数据
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:edit")]
        [Log(Title = "采集曲线数据", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksCollectedCurveData([FromBody] CksCollectedCurveDataDto parm) {
            var modal = parm.Adapt<CksCollectedCurveData>().ToUpdate(HttpContext);
            var response = _CksCollectedCurveDataService.UpdateCksCollectedCurveData(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除采集曲线数据
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:delete")]
        [Log(Title = "采集曲线数据", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksCollectedCurveData([FromRoute] string ids) {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CksCollectedCurveDataService.Delete(idArr));
        }


        /// <summary>
        /// 导出采集曲线数据
        /// </summary>
        /// <returns></returns>
        [Log(Title = "采集曲线数据", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "ckscollectedcurvedata:export")]
        public IActionResult Export([FromQuery] CksCollectedCurveDataQueryDto parm) {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _CksCollectedCurveDataService.ExportList(parm).Result;
            if (list == null || list.Count <= 0) {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }

            //{"PrimaryCode":"P1693247-20-F:SSKS25213000063","Channel":13,"PressureChannel":"161.34984","TemperatureChannel":"34.4","CreateTime":"2025-09-14T17:27:34.9552771+08:00"}
            Dictionary<string, object> keyValues = new Dictionary<string, object>();
            keyValues.Add("采集曲线数据", list.Select(o => new {
                SN = o.PrimaryCode,
                CollectBatchCode = o.CollectBatchCode, 
                Channel = JObject.Parse(o.CurveData)["Channel"],
                Pressure = JObject.Parse(o.CurveData)["PressureChannel"],
                Temperature = JObject.Parse(o.CurveData)["TemperatureChannel"],
                CreateTime = JObject.Parse(o.CurveData)["CreateTime"],
            }));
            var result = ExportExcelMini(keyValues, "采集曲线数据");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}